package com.woniuxy.driverschool.filter;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.driverschool.entity.Message4D2admin;
import com.woniuxy.driverschool.entity.Store;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMethod;


import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author shichen
 * @create 2021-10-14-23:14
 * @description
 */
@Component
public class MyFormAuthenticationFilter extends FormAuthenticationFilter {

    private static final Log log = LogFactory.get();

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        log.info(">>>>>preHandle");
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        res.setHeader("Access-control-Allow-Origin", req.getHeader("Origin"));
        res.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
        res.setHeader("Access-Control-Allow-Headers", req.getHeader("Access-Control-Request-Headers"));
        // 跨域时会首先发送一个option请求，这里我们给option请求直接返回正常状态
        if (req.getMethod().equals(RequestMethod.OPTIONS.name())) {
            log.info(">>>>>MyFormAuthenticationFilter---OPTIONS");
            //res.setStatus(HttpStatus.OK.value());
            res.setStatus(HttpStatus.NO_CONTENT.value());
            return false;
        }
        log.info(">>>>>preHandle end");
        return super.preHandle(req, res);
    }
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        try {
            log.info(" 开始 isAccessAllowed");
            //运行访问
            super.isAccessAllowed(request, response, mappedValue);
            log.info(" mappedValue: {}",mappedValue);
//            HttpServletRequest req = (HttpServletRequest) request;
//            String token =  req.getHeader("Authorization");
//
//            log.info("get token : {} " , token);
//
//            Subject subject = (Subject)Store.getMap().get(token);
//            log.info("get subject from server {}",subject);
//
//            if(!subject.isPermitted("user:select")){
//                log.info("没有授权");
//                throw new RuntimeException("没有授权");
//            }

        } catch (Exception e) {
            //出现异常，就是不许访问，后端给前端JSON信息
            HttpServletResponse res = (HttpServletResponse) response;
            res.setContentType("text/html;charset=utf-8");
            try {
                new ObjectMapper().writeValue(response.getWriter(),
                        new Message4D2admin(null,401,"先登录"));
            } catch (IOException ex) {
                ex.printStackTrace();
                return false;
            }
        }
        log.info("isAccessAllowed 放行");
        return true;
    }
}
